<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../style/fjol_style.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../style/xol_style.css"/>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav newbgRed">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">选择楼盘</h1>
		</header>
		<div class="mui-content">
		<div class="mui-input-row bg-white" style="margin: 10px 15px; border-radius: 6px;">
		    <label class="mui-text-center newRed" id="searchBtn" style="float: right; width: 20%;">搜索</label>
		    <input type="text" placeholder="请输入楼盘名称" id="searchipt" value="">
		</div>
		    <div class="mui-scroll-wrapper" style="top: 100px;">
			    <div id="pullrefresh" class="mui-scroll">
			        <!--这里放置真实显示的DOM内容-->
			        <ul class="mui-table-view" id="buildingNameList">
			                
			            </ul>
			    </div>
			</div>
		</div>
		    
		<nav class="mui-bar mui-bar-tab mui-hidden">
		    <button type="button" class="mui-btn mui-btn-blue mui-btn-block" id="addBuildName">找不到相应楼盘？点击直接添加</button>
		</nav>
		<script src="../js/mui.min.js"></script>
		<script src="../js/jquery-2.1.0.js"></script>
		<script src="../js/jquery.tmpl.min.js"></script>
		<script src="../js/md5.js"></script>
		<script src="../js/common.util.js"></script>
		<script src="../js/network.js"></script>
		<script src="../js/mui.pullToRefresh.js"></script>
		<script src="../js/mui.pullToRefresh.material.js"></script>
		<script id="buildingNameTmpl" type="text/x-jquery-tmpl">
			<li class="mui-table-view-cell" id="${id}">
                <span>${name}</span>
                <p>${address}</p>
            </li>
		</script>
		<script type="text/javascript">
			mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					down: {
						callback: pulldownRefresh
					},
					up: {
						contentrefresh: '正在加载...',
						callback: pullupRefresh
					}
				}
			});
			var pagevalue = 1;
			function pulldownRefresh(){
//				console.log(2);
				var searchValue = document.querySelector('#searchipt').value;
				$('.mui-pull').hide();
				setTimeout(function(){
					getBuildingName(searchValue,1,true);
					mui('#pullrefresh').pullRefresh().endPullupToRefresh();
				 	mui('#pullrefresh').pullRefresh().enablePullupToRefresh();//重新开启上拉加载
					return pagevalue = 1;
				},1000);
				
			}
			function pullupRefresh(){
				var searchValue = document.querySelector('#searchipt').value;
				setTimeout(function(){
					pagevalue++;
					getBuildingName(searchValue,pagevalue,false);
				},1000);
			}
			mui.plusReady(function(){
				var wv = plus.webview.currentWebview();
				var parentId = wv.parentId;
				var _type = wv._type;
//				var fj_house_issue_sub = plus.webview.getWebviewById('houseAppraisal');
				var parentPage = plus.webview.getWebviewById(parentId);
//				var houseAppraisal = plus.webview.getWebviewById('fj_house_appraisal');
				
//				var rentHouse = plus.webview.getWebviewById('rent_house');	//租房
				
//				console.log(_type);		//1、发布房源；2、租房；3、房屋估价
				
//				console.log(parentId);
				document.querySelector('#searchBtn').addEventListener('tap',function(){
					var searchValue = document.querySelector('#searchipt').value;
					if(searchValue==''){
						mui.alert('请输入楼盘关键词');
						return false;
					}
					getBuildingName(searchValue,1,true);
				});
				
				document.getElementById("searchipt").addEventListener("keyup",function(e){ 
					var searchInput = this.value;
			        getBuildingName(searchInput,1,true);
			    },false); 
				document.getElementById("searchipt").addEventListener("keydown",function(e){ 
			        if(13 == e.keyCode){ //点击了“搜索”  
			           document.activeElement.blur();//隐藏软键盘 
			           var searchInput = document.querySelector('#searchipt').value;
//			           console.log(searchInput);
			           getBuildingName(searchInput,1,true);
			        } 
			    },false); 
    
				mui('#buildingNameList').on('tap','li',function(){
//					console.log(this.childNodes[1].innerHTML);
//					if(_type == 1){		//发布房源
////						console.log(1);
//						mui.fire(fj_house_issue_sub,'getBuilding',{
//							buildingId:this.getAttribute('id'),
//							buildingName:this.childNodes[1].innerHTML
//						});
//					}
//					if(_type == 3){
////						console.log(3);
//						mui.fire(fj_house_issue_sub,'getBuilding',{
//							buildingId:this.getAttribute('id'),
//							buildingName:this.childNodes[1].innerHTML
//						});
//					}
//					if(_type == 2){		//租房
////						console.log(2);
//						mui.fire(fj_house_issue_sub,'getBuilding',{
//							buildingId:this.getAttribute('id'),
//							buildingName:this.childNodes[1].innerHTML
//						});
//					}else{
						mui.fire(parentPage,'getBuilding',{
							buildingId:this.getAttribute('id'),
							buildingName:this.childNodes[1].innerHTML
						});
//					}
					mui.back();
				});
				
				//找不到楼盘，自己添加
				document.querySelector('#addBuildName').addEventListener('tap',function(){
					if(parentId == 'houseIssue'){
						mui.fire(houseIssue,'getBuilding',{
							buildingId:'',
							buildingName:document.querySelector('#searchipt').value
						});
					}
					if(parentId == 'houseAppraisal'){
//						console.log(2);
						mui.fire(houseAppraisal,'getBuilding',{
							buildingId:'',
							buildingName:document.querySelector('#searchipt').value
						});
					}else{
						mui.fire(parentPage,'getBuilding',{
							buildingId:'',
							buildingName:document.querySelector('#searchipt').value
						});
					}
					mui.back();
				});
			})
			
			function getBuildingName(searchValue,pagevalue,isTrue){
				var param = [{
					key:'searchKey',
					value:searchValue
				},{
					key:'page',
					value:pagevalue
				},{
					key:'rows',
					value:10
				}];
				fjNetwork.get('fjzx/common/searchBuilding.json',param,function(data){
//					console.log(JSON.stringify(data));
					var rows = data.rows;
					if(rows.length==0){
						document.querySelector('nav').classList.remove('mui-hidden');
					}
					if(isTrue){
						document.querySelector('#buildingNameList').innerHTML = '';
					}
					$('#buildingNameTmpl').tmpl(rows).appendTo('#buildingNameList');
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(rows.length==0);
				},function(msg){
//					console.log(msg);
					mui.toast(msg);
				});
			}
		</script>
	</body>

</html>